<?php
$v_data['stock_id'] = '220083';
$v_data['shop_id'] = '250715';
$v_data['owner_id'] = '260653';

/* 商品导入 */
function vsetpushProducts($goodsIdList='', $v_data = ''){
    if(!is_array($goodsIdList) && !$goodsIdList){
        $goodsIdList = $GLOBALS['db']->getCol('SELECT goods_id FROM ' . $GLOBALS['ecs']->table('goods_stock') . " WHERE area_id = 3 AND goods_attr_id = 0 GROUP BY goods_id");
        // $goodsIdList = array_slice($goodsIdList,0,30);
        //sku:8020,8041,8047,8048,8061,8070,8072,8075
    }
    if(is_array($goodsIdList)){
        $v_data['count'] = 0;
        foreach ($goodsIdList as $key => $value) {
            $sql = "SELECT g.goods_name, g.goods_sku, g.goods_thumb, wstc.* FROM " . $GLOBALS['ecs']->table('goods') . " g LEFT JOIN " . $GLOBALS['ecs']->table('wcskutocode') . " wstc ON wstc.sku = g.goods_sku WHERE is_delete = 0 AND goods_id = " . intval($value);
            $goodsInfo = $GLOBALS['db']->getRow($sql);
            if($goodsInfo){
                $vgoodsInfo = array();
                $vgoodsInfo['owner_id'] = $v_data['owner_id'];
                $vgoodsInfo['product_id'] = $goodsInfo['goods_sku'];
                $vgoodsInfo['product_name'] = $goodsInfo['goods_name'];
                $vgoodsInfo['product_title'] = $goodsInfo['goods_name'];
                $vgoodsInfo['remark'] = 'eggms';
                $sql = "SELECT ga.attr_value AS label, ga.attr_sku AS goods_attr_sku, wstc.* FROM " . $GLOBALS['ecs']->table('goods_attr') . " ga LEFT JOIN " . $GLOBALS['ecs']->table('wcskutocode') . " wstc ON wstc.sku = ga.attr_sku WHERE goods_id = " . intval($value);
                $attr = $GLOBALS['db']->getAll($sql);
                if($attr){
                    foreach ($attr as $k => $v) {
                        $vgoodsInfo['skus'][$k]['sku_id'] = $v['goods_attr_sku'];
                        $vgoodsInfo['skus'][$k]['sku_name'] = $v['label'];
                        $vgoodsInfo['skus'][$k]['sku_img_url'] = 'http://admin.eggms.com/'.$goodsInfo['goods_thumb'];
                        $vgoodsInfo['skus'][$k]['product_code'] = "$v[code]";
                        $vgoodsInfo['skus'][$k]['flammable'] = '0';
                        $vgoodsInfo['skus'][$k]['explosive'] = '0';
                        $vgoodsInfo['skus'][$k]['liquid'] = '1';
                        $vgoodsInfo['skus'][$k]['has_barcode'] = '1';
                    }
                }else{
                    $vgoodsInfo['skus'][0]['sku_id'] = $goodsInfo['goods_sku'];
                    $vgoodsInfo['skus'][0]['sku_name'] = '无';
                    $vgoodsInfo['skus'][0]['sku_img_url'] = 'http://admin.eggms.com/'.$goodsInfo['goods_thumb'];
                    $vgoodsInfo['skus'][0]['product_code'] = "$goodsInfo[code]";
                    $vgoodsInfo['skus'][0]['flammable'] = '0';
                    $vgoodsInfo['skus'][0]['explosive'] = '0';
                    $vgoodsInfo['skus'][0]['liquid'] = '1';
                    $vgoodsInfo['skus'][0]['isBarcode'] = '1';
                }
                $v_data['count'] += 1;
                $v_data['products'][] = $vgoodsInfo;
            }
        }
    }
    if($v_data['count']){
        return vpost(vgeturl('pushProducts'),vdata($v_data));
    }
    return $v_data;
}

/* 销售退货单导入 */
function vsetpushBackTrades($orderIdList='', $v_data = ''){
    if(is_array($orderIdList)){
        $v_data['total_count'] = '0';
        foreach ($orderIdList as $key => $value) {
            $orderInfo = back_info($value);
            if($orderInfo){
                if($orderInfo['goodsList']){
                    $vorderInfo['order_return_no'] = $value;
                    $vorderInfo['order_no'] = $orderInfo['order_sn'];
                    $vorderInfo['stock_id'] = $v_data['stock_id'];
                    $vorderInfo['transporter_id'] = $orderInfo['express_id'];
                    $vorderInfo['out_sid'] = $orderInfo['express_sn'];
                    $vorderInfo['buyer_nick'] = $orderInfo['return_id'];
                    $vorderInfo['receiver_name'] = '网仓';
                    $vorderInfo['receiver_mobile'] = '0756-7683366';
                    $vorderInfo['shop_id'] = $v_data['shop_id'];
                    foreach ($orderInfo['goodsList'] as $k => $v) {
                        $vorderInfo['skus'][$k]['product_code'] = $v['code'];
                        $vorderInfo['skus'][$k]['qty'] = $v['goods_number'];
                        $vorderInfo['skus'][$k]['shop_id'] = $v_data['shop_id'];
                        $vorderInfo['skus'][$k]['owner_id'] = $v_data['owner_id'];
                    }
                    $v_data['total_count'] += 1;
                    $v_data['backTrades'][] = $vorderInfo;
                    unset($vorderInfo);
                }
            }
        }
    }
    if($v_data['total_count']){
        unset($v_data['shop_id']);
        unset($v_data['owner_id']);
        unset($v_data['stock_id']);
        $v_data['total_count'] = strval($v_data['total_count']);
        return vpost(vgeturl('pushBackTrades'),vdata($v_data));
    }
    return $v_data;
}

/* 订单导入 */
function vsetpushTrades($orderIdList='', $v_data = ''){
    if(is_array($orderIdList)){
        $v_data['count'] = 0;
        foreach ($orderIdList as $key => $value) {
            $orderInfo = order_info($value);
            if($orderInfo && $orderInfo['order_status'] == 1 &&($orderInfo['pay_id'] == 3 || ($orderInfo['pay_id'] == 1 && $orderInfo['pay_status'] == 2))){
                $goodsList = orderGoods($value);
                if($goodsList){
                    $vorderInfo = array();
                    $vorderInfo['order_no'] = $orderInfo['order_sn'];
                    $vorderInfo['stock_id'] = $v_data['stock_id'];
                    $vorderInfo['shop_id'] = $v_data['shop_id'];
                    $vorderInfo['transporter_flag'] = '0';
                    $vorderInfo['order_create_time'] = date('Y-m-d H:i:s', $orderInfo['add_time']);
                    $vorderInfo['order_pay_time'] = $orderInfo['pay_id']==3 ? date('Y-m-d H:i:s') : date('Y-m-d H:i:s', $orderInfo['pay_time']);
                    $vorderInfo['is_cod'] = $orderInfo['pay_id']==3 ? '1' : '0';
                    $vorderInfo['cod_fee'] = $orderInfo['pay_id']==3 ? $orderInfo['total_fee'] : '0';
                    $vorderInfo['order_status'] = $orderInfo['pay_id']==3 ? '1' : '0';
                    $vorderInfo['buyer_nick'] = $orderInfo['user_name'];
                    $vorderInfo['receiver_country'] = selectaddress($orderInfo['country']);
                    $vorderInfo['receiver_province'] = selectaddress($orderInfo['province']);
                    $vorderInfo['receiver_city'] = selectaddress($orderInfo['city']);
                    $vorderInfo['receiver_county'] = selectaddress($orderInfo['district']);
                    $vorderInfo['receiver_address'] = $orderInfo['address'];
                    $vorderInfo['receiver_name'] = $orderInfo['consignee'];
                    $vorderInfo['receiver_mobile'] = $orderInfo['mobile'];
                    $vorderInfo['total_fee'] = $orderInfo['total_fee'];
                    $vorderInfo['payment'] = $orderInfo['total_fee'];
                    $vorderInfo['need_invoice'] = 0;
                    $vorderInfo['need_card'] = 0;

                    foreach ($goodsList as $k => $v) {
                        if (!empty($v['code'])) {
                            $vorderInfo['skus'][$k]['product_code'] = $v['code'];
                            $vorderInfo['skus'][$k]['qty'] = $v['goods_number'];
                            $vorderInfo['skus'][$k]['payment'] = $v['goods_number']*$v['goods_price'];
                            $vorderInfo['skus'][$k]['discount_fee'] = 0;
                            $vorderInfo['skus'][$k]['shop_id'] = $v_data['shop_id'];
                            $vorderInfo['skus'][$k]['owner_id'] = $v_data['owner_id'];
                            $vorderInfo['skus'][$k]['sell_price'] = $v['goods_price'];
                        }
                    }
                    $vorderInfo['skus'] = array_values($vorderInfo['skus']);
                    $v_data['count'] += 1;
                    $v_data['trades'][] = $vorderInfo;
                    unset($vorderInfo);
                }
            }
        }
    }
    if($v_data['count']){
        return vpost(vgeturl('pushTrades'),vdata($v_data));
    }
    return $v_data;
}

/* 订单发货查询 */
function vgettradeDeliverQuery($orderSnList='', $v_data = '', $time_type = 1, $begin_create_date = '', $end_create_date = ''){
    if($orderSnList && is_array($orderSnList)){
        foreach ($orderSnList as $key => $value) {
            $v_data['order_no'] = $value;
            $data[$key] = vpost(vgeturl('tradeDeliverQuery'),vdata($v_data));
        }
        return $data;
    }else{
        $v_data['time_type'] = $time_type;
        $v_data['begin_date'] = $begin_create_date;
        $v_data['end_date'] = $end_create_date;
        $v_data['page_size'] = 1000;
        $v_data['page_no'] = 1;
        $data = vpost(vgeturl('tradeDeliverQuery'),vdata($v_data));
        return $data;
    }
    return $v_data;
}

/* 订单状态查询 */
function vgettradeStatusQuery($orderSnList='', $v_data = '', $time_type = 1, $begin_create_date = '', $end_create_date = ''){
    if($orderSnList && is_array($orderSnList)){
        foreach ($orderSnList as $key => $value) {
            $v_data['order_no'] = $value;
            $data[$key] = vpost(vgeturl('tradeStatusQuery'),vdata($v_data));
        }
        return $data;
    }else{
        $v_data['time_type'] = $time_type;
        $v_data['begin_date'] = $begin_create_date;
        $v_data['end_date'] = $end_create_date;
        $v_data['page_size'] = 1000;
        $v_data['page_no'] = 1;
        $data = vpost(vgeturl('tradeStatusQuery'),vdata($v_data));
        return $data;
    }
    return $v_data;
}

/* 订单称重查询 */
function vgettradeWeightQuery($orderSnList='', $v_data = ''){
    if($orderSnList && is_array($orderSnList)){
        foreach ($orderSnList as $key => $value) {
            $v_data['order_no'] = $value;
            $data[$key] = vpost(vgeturl('tradeWeightQuery'),vdata($v_data));
        }
        return $data;
    }
    return $v_data;
}

/* 订单查询 */
function vgettradeQuery($orderSnList='', $v_data = '', $time_type = 1, $begin_create_date = '', $end_create_date = ''){





    if($orderSnList && is_array($orderSnList)){
        foreach ($orderSnList as $key => $value) {
            $v_data['order_no'] = $value;
//             echo "<pre>";
//             print_r(vpost(vgeturl('tradeQuery'),vdata($v_data)));
//             echo "</pre>";
// exit();
            $data[$key] = vpost(vgeturl('tradeQuery'),vdata($v_data));
            // dump(vpost(vgeturl('tradeQuery'),vdata($v_data)));
        }
        return $data;
    }
    else{
        $v_data['time_type'] = $time_type;
        $v_data['begin_date'] = $begin_create_date;
        $v_data['end_date'] = $end_create_date;
        $v_data['page_size'] = 1000;
        $v_data['page_no'] = 1;
        $data = vpost(vgeturl('tradeQuery'),vdata($v_data));
        return $data;
    }
    return $v_data;
}
/* 订单查询 */
function vgettradeQuery2($orderSnList='', $v_data = '', $time_type = 1, $begin_create_date = '', $end_create_date = ''){
    if($orderSnList && is_array($orderSnList)){
        foreach ($orderSnList as $key => $value) {
            $v_data['iscs_order_no'] = $value;
            $data[$key] = vpost(vgeturl('tradeQuery'),vdata($v_data));
        }
        return $data;
    }
    else{
        $v_data['time_type'] = $time_type;
        $v_data['begin_date'] = $begin_create_date;
        $v_data['end_date'] = $end_create_date;
        $v_data['page_size'] = 1000;
        $v_data['page_no'] = 1;
        $data = vpost(vgeturl('tradeQuery'),vdata($v_data));
        return $data;
    }
    return $v_data;
}
/* 订单取消 */
function vgetcancelTrade($orderSnList='', $v_data = ''){
    if($orderSnList && is_array($orderSnList)){
        foreach ($orderSnList as $key => $value) {
            $v_data['order_no'] = $value;
            $data[$key] = vpost(vgeturl('cancelTrade'),vdata($v_data));
        }
        return $data;
    }
    return $v_data;
}

/* 订单操作日志查询 */
function vgettradeLogQuery($orderSnList='', $v_data = ''){
    if($orderSnList && is_array($orderSnList)){
        foreach ($orderSnList as $key => $value) {
            $v_data['order_no'] = $value;
            $data[$key] = vpost(vgeturl('tradeLogQuery'),vdata($v_data));
        }
        return $data;
    }
    return $v_data;
}

/* 退货单取消 */
function vsetcancelBackTrade($backId='', $v_data = '', $remark = ''){
    if($backId && is_array($backId)){
        foreach ($backId as $key => $value) {
            $v_data['order_return_no'] = $value;
            $v_data['remark'] = $remark;
            $data[$key] = vpost(vgeturl('cancelBackTrade'),vdata($v_data));
        }
        return $data;
    }
    return $v_data;
}

/* 退货单入库数据查询 */
function vgetbackTradeInStockQuery($backId='', $v_data = '', $remark = ''){
    if($backId && is_array($backId)){
        foreach ($backId as $key => $value) {
            $v_data['order_return_no'] = $value;
            $data[$key] = vpost(vgeturl('backTradeInStockQuery'),vdata($v_data));
        }
        return $data;
    }
    return $v_data;
}

/* 退货查询 */
function vgetbackTradeQuery($backId='', $v_data = '', $remark = ''){
    if($backId && is_array($backId)){
        foreach ($backId as $key => $value) {
            $v_data['order_return_no'] = $value;
            $data[$key] = vpost(vgeturl('backTradeQuery'),vdata($v_data));
        }
        return $data;
    }
    return $v_data;
}

/* 库存查询 */
function vgetinventoryQuery($goodsSkuList = '', $v_data = '', $remark = ''){
    unset($v_data['shop_id']);
    if($goodsSkuList && is_array($goodsSkuList)){
        foreach ($goodsSkuList as $key => $value) {
            $v_data['product_code'] = $value;
            $data[$key] = vpost(vgeturl('inventoryQuery'),vdata($v_data));
        }
        return $data;
    }
    if($goodsSkuList){
        $v_data['page_no'] = "$goodsSkuList";
        $v_data['page_size'] = '100000';
        $v_data['product_code'] = '';
        $data = vpost(vgeturl('inventoryQuery'),vdata($v_data));
        return $data;
    }
    return $v_data;
}

/* 权限查询 */
function vgetauthorityQuery($v_data = ''){
    return vpost(vgeturl('authorityQuery'),vdata($v_data));
}


/* 处理请求数据 */
function vdata($data = ''){
    // require_once(ROOT_PATH . 'includes/cls_json.php');
    // $json = new JSON();
    // echo "<pre>";
    // print_r('v_data='.$json->encode($data));
    // echo "</pre>";
    // exit;
    if(is_array($data)){
        require_once(ROOT_PATH . 'includes/cls_json.php');
        $json = new JSON();

        $data = 'v_data='.urlencode($json->encode($data));
    }else{
        $data = 'v_data='.urlencode($data);
    }
    return $data;
}

/* 获取请求地址 */
function vgeturl($v_method = ''){
    $v_appkey = '99020199';
    $v_appSecret = '601400004500230149562454';
    $v_timestamp = date('Y-m-d%20H:i:s');
    $v_appsign = strtoupper(MD5($v_appkey.$v_appSecret.date('Y-m-d H:i:s')));
    $v_method = $v_method;
    $v_format = 'json';
    $v_url = "http://ecmapi.iscs.com.cn/openapi/do?v_appkey=$v_appkey&v_appsign=$v_appsign&v_format=json&v_timestamp=$v_timestamp&v_method=$v_method";
    return $v_url;
}


/**
 * 邮件发送
 *
 * @param: $url[string]        请求地址
 * @param: $data[json]       请求数据
 *
 * @return $tmpInfo 数据
 */
function vpost($url,$data=""){ // 模拟提交数据函数
    $curl = curl_init(); // 启动一个CURL会话
    curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); // 模拟用户使用的浏览器
    // curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
    // curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
    curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包x
    curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
    curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
    $tmpInfo = curl_exec($curl); // 执行操作
    if (curl_errno($curl)) {
       echo 'Errno'.curl_error($curl);//捕抓异常
    }
    curl_close($curl); // 关闭CURL会话
    require_once(ROOT_PATH . 'includes/cls_json.php');
    $json = new JSON();
    return $json->decode($tmpInfo,true); // 返回数据
}


/*退货单详情*/
function back_info($return_id){
    $sql = "SELECT * FROM " . $GLOBALS['ecs']->table('order_return_info') . " WHERE return_id = $return_id ";
    $return_info = $GLOBALS['db']->getRow($sql);
    $return_info['add_time'] =date('Y-m-d-h:m:s',$return_info['add_time']);
    $sql = "SELECT * FROM " . $GLOBALS['ecs']->table('order_return_goods') . " org LEFT JOIN " . $GLOBALS['ecs']->table('wcskutocode') . " wstc ON wstc.sku = IF(org.goods_attr_sku,org.goods_attr_sku,org.goods_sku) WHERE return_id = $return_id ";
    $return_goods = $GLOBALS['db']->getAll($sql);
    $return_info['goodsList']=$return_goods;
    return $return_info;
}

/**
 * 取得订单商品
 * @param   int     $order_id   订单id
 * @return  array   订单商品数组
 */
function orderGoods($order_id)
{
    $count="";
    $sql = "SELECT o.rec_id, o.goods_id, o.goods_name, o.goods_sku, o.market_price, o.goods_number, g.goods_thumb, g.goods_img, " .
            "o.goods_price, o.goods_attr, o.is_real, o.parent_id, o.is_gift, g.give_integral, " .
            "o.goods_price * o.goods_number AS subtotal, o.extension_code, o.goods_attr_sku, wstc.code, wstc.sku, wstc.isBarcode " .
            "FROM " . $GLOBALS['ecs']->table('order_goods') . " o ".
            "LEFT JOIN " . $GLOBALS['ecs']->table('goods') . " g ON g.goods_id = o.goods_id " .
            "LEFT JOIN " . $GLOBALS['ecs']->table('wcskutocode') . " wstc ON wstc.sku = IF(o.goods_attr_sku,o.goods_attr_sku,g.goods_sku) " .
            " WHERE o.order_id = '$order_id'";

    $res = $GLOBALS['db']->query($sql);
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        if ($row['extension_code'] == 'libao') {
            if ($row['goods_id'] == 0 && !empty($row['parent_id'])) {
                /* 查询礼包商品信息 */
                $sql = "SELECT ag.goods_number, g.goods_id, g.goods_sku, g.goods_name, g.market_price, g.shop_price, ga.goods_attr_id, ga.attr_sku AS goods_attr_sku, ga.attr_value AS goods_attr, wstc.code, wstc.isBarcode FROM " . $GLOBALS['ecs']->table('activity_goods') . " ag LEFT JOIN " . $GLOBALS['ecs']->table('goods') . " g ON g.goods_id = ag.goods_id LEFT JOIN " . $GLOBALS['ecs']->table('goods_attr') . " ga ON ga.goods_attr_id = ag.goods_attr_id".
                " LEFT JOIN " . $GLOBALS['ecs']->table('wcskutocode') . " wstc ON wstc.sku = IF(ga.attr_sku,ga.attr_sku,g.goods_sku) " .
                " WHERE parent_id = '$row[parent_id]'";
                $gList = $GLOBALS['db']->getAll($sql);
                foreach ($gList as $key => $value) {
                    $parent                   = $value;
                    $parent['goods_price']    = $value['shop_price'];
                    $parent['goods_number']   = $value['goods_number'] * $row['goods_number'];
                    $parent['formated_subtotal']  = price_format($row['goods_price'] * $parent['goods_number']);
                    $parent['formated_goods_price']    = price_format($row['goods_price']);
                    $goods_list[] = $parent;
                }
                $goods_list[] = $row;
            }
        } elseif ($row['extension_code'] == 'gift') {
            if ($row['goods_id'] == 0 && !empty($row['parent_id'])) {
                /* 查询礼包商品信息 */
                $sql = "SELECT ag.goods_number, g.goods_id, g.goods_sku, g.goods_name, g.market_price, g.shop_price, ga.goods_attr_id, ga.attr_sku AS goods_attr_sku, ga.attr_value AS goods_attr, wstc.code, wstc.isBarcode FROM " . $GLOBALS['ecs']->table('activity_goods') . " ag LEFT JOIN " . $GLOBALS['ecs']->table('goods') . " g ON g.goods_id = ag.goods_id LEFT JOIN " . $GLOBALS['ecs']->table('goods_attr') . " ga ON ga.goods_attr_id = ag.goods_attr_id".
                " LEFT JOIN " . $GLOBALS['ecs']->table('wcskutocode') . " wstc ON wstc.sku = IF(ga.attr_sku,ga.attr_sku,g.goods_sku) " .
                " WHERE parent_id = '$row[parent_id]'";
                $gList = $GLOBALS['db']->getAll($sql);
                foreach ($gList as $key => $value) {
                    $parent                   = $value;
                    $parent['goods_price']    = $value['shop_price'];
                    $parent['goods_number']   = $value['goods_number'] * $row['goods_number'];
                    $parent['formated_subtotal']  = price_format($row['goods_price'] * $parent['goods_number']);
                    $parent['formated_goods_price']    = price_format($row['goods_price']);
                    $goods_list[] = $parent;
                }
                $goods_list[] = $row;
            }
        } else {
            $goods_list[] = $row;
        }
    }
    if(!empty($goods_list)){
        foreach ($goods_list as $key => $value) {
            $count += $value["goods_number"];
        }
    }
    $goods_list[0]['count']= $count;
    //return $GLOBALS['db']->getAll($sql);
    return $goods_list;
}